﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;
using RibbonEditor.AppCode;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class MRUSplitButtonControl : UserControl, IControlRulable
    {
        private MRUSplitButton innerButton;

        public MRUSplitButton InnerButton
        {
            get { return innerButton; }
            set { innerButton = value; }
        }

        public MRUSplitButtonControl()
        {
            InitializeComponent();
            this.innerButton = new MRUSplitButton();
            this.FillControls();
        }

        public MRUSplitButtonControl(MRUSplitButton button):this()
        {
            this.innerButton = button;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcAlt.Value = this.innerButton.Alt;
                this.bcCacheMenuVersions.Value = BoolHelper.GetBoolValue(this.innerButton.CacheMenuVersions);
                this.rcCommand.Value = this.innerButton.Command;
                this.rcCommandMenuClose.Value = this.innerButton.CommandMenuClose;
                this.rcCommandMenuOpen.Value = this.innerButton.CommandMenuOpen;
                this.rcCommandPreview.Value = this.innerButton.CommandPreview;
                this.rcCommandPreviewRevert.Value = this.innerButton.CommandPreviewRevert;
                this.strId.Value = this.innerButton.Id;
                this.strInitialItem.Value = this.innerButton.InitialItem;
                this.rcMenuAlt.Value = this.innerButton.MenuAlt;
                this.rcMenuCommand.Value = this.innerButton.MenuCommand;
                this.bcPopulateDynamically.Value = BoolHelper.GetBoolValue(this.innerButton.PopulateDynamically);
                this.bcPopulateOnlyOnce.Value = BoolHelper.GetBoolValue(this.innerButton.PopulateOnlyOnce);
                this.strPopulateQueryCommand.Value = this.innerButton.PopulateQueryCommand;
                this.strQueryCommand.Value = this.innerButton.QueryCommand;
                this.icSequence.Value = this.innerButton.Sequence;
                this.strTemplateAlias.Value = this.innerButton.TemplateAlias;
                this.rcToolTipDescription.Value = this.innerButton.ToolTipDescription;
                this.strToolTipKeyWord.Value = this.innerButton.ToolTipHelpKeyWord;
                this.rcToolTipImage32by32.Value = this.innerButton.ToolTipImage32by32;
                this.strToolTipImage32by32Class.Value = this.innerButton.ToolTipImage32by32Class;
                this.icToolTipImage32by32Left.Value = this.innerButton.ToolTipImage32by32Left;
                this.icToolTipImage32by32Top.Value = this.innerButton.ToolTipImage32by32Top;
                this.strToolTipShortcutKey.Value = this.innerButton.ToolTipShortcutKey;
                this.rcToolTipTitle.Value = this.innerButton.ToolTipTitle;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerButton.Alt = this.rcAlt.Value;
                    this.innerButton.CacheMenuVersions = this.bcCacheMenuVersions.Value;
                    this.innerButton.Command = this.rcCommand.Value;
                    this.innerButton.CommandMenuClose = this.rcCommandMenuClose.Value;
                    this.innerButton.CommandMenuOpen = this.rcCommandMenuOpen.Value;
                    this.innerButton.CommandPreview = this.rcCommandPreview.Value;
                    this.innerButton.CommandPreviewRevert = this.rcCommandPreviewRevert.Value;
                    this.innerButton.Id = this.strId.Value;
                    this.innerButton.InitialItem = this.strInitialItem.Value;
                    this.innerButton.MenuAlt = this.rcMenuAlt.Value;
                    this.innerButton.MenuCommand = this.rcMenuCommand.Value;
                    this.innerButton.PopulateDynamically = this.bcPopulateDynamically.Value;
                    this.innerButton.PopulateOnlyOnce = this.bcPopulateOnlyOnce.Value;
                    this.innerButton.PopulateQueryCommand = this.strPopulateQueryCommand.Value;
                    this.innerButton.QueryCommand = this.strQueryCommand.Value;
                    this.innerButton.Sequence = this.icSequence.Value;
                    this.innerButton.TemplateAlias = this.strTemplateAlias.Value;
                    this.innerButton.ToolTipDescription = this.rcToolTipDescription.Value;
                    this.innerButton.ToolTipHelpKeyWord = this.strToolTipKeyWord.Value;
                    this.innerButton.ToolTipImage32by32 = this.rcToolTipImage32by32.Value;
                    this.innerButton.ToolTipImage32by32Class = this.strToolTipImage32by32Class.Value;
                    this.innerButton.ToolTipImage32by32Left = this.icToolTipImage32by32Left.Value;
                    this.innerButton.ToolTipImage32by32Top = this.icToolTipImage32by32Top.Value;
                    this.innerButton.ToolTipShortcutKey = this.strToolTipShortcutKey.Value;
                    this.innerButton.ToolTipTitle = this.rcToolTipTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void MRUSplitButtonControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerButton.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
